package com.lottery.shadowbd.ui.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.bean.LotTrendBean;
import com.lottery.shadowbd.bean.Rule;
import com.lottery.shadowbd.callback.ECallback;
import com.lottery.shadowbd.io.api.LotApiIO;
import com.lottery.shadowbd.tuple.Tuple2;
import com.lottery.shadowbd.utils.ToastUtil;
import com.lottery.shadowbd.widget.DDTrendChart;
import com.lottery.shadowbd.widget.LottoTrendView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * 趋势图
 * A simple {@link Fragment} subclass.
 */
public class LotTrendFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg_tab_msg;

    /** 双色球 */
    private LotSSQTrendFragment mLOLotSSQTrendFragment;
    /** 大乐透 */
    private LotDLTTrendFragment mLOLotDLTTrendFragment;
    private TextView tv_trend_intro;

    private Fragment currentFragment;

    private FragmentTransaction mTransaction;

    private View mView;

    private String intro;

    @Override
    protected View initView(LayoutInflater inflater) {
        mView = inflater.inflate(R.layout.fragment_lot_trend, null);

        rg_tab_msg = (RadioGroup) mView.findViewById(R.id.rg_tab_msg);
        tv_trend_intro = (TextView) mView.findViewById(R.id.tv_trend_intro);

        return mView;
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        initFragment();
    }

    @Override
    protected void listener() {
        tv_trend_intro.setOnClickListener(this);
        rg_tab_msg.setOnCheckedChangeListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_trend_intro://说明
                showIntro();
                break;
            default:
                break;
        }
    }

    private void initFragment() {
        mLOLotSSQTrendFragment = new LotSSQTrendFragment();

        currentFragment = mLOLotSSQTrendFragment;

        mTransaction = getChildFragmentManager().beginTransaction();

        mTransaction.add(R.id.frame_lot_trend, mLOLotSSQTrendFragment);
        mTransaction.commit();
    }

    //双色球
    private void switchToleft() {
        if(mLOLotSSQTrendFragment == null) {
            mLOLotSSQTrendFragment = new LotSSQTrendFragment();
        }

        switchFragment(mLOLotSSQTrendFragment);
    }

    //大乐透
    private void switchToRight() {
        if(mLOLotDLTTrendFragment == null) {
            mLOLotDLTTrendFragment = new LotDLTTrendFragment();
        }

        switchFragment(mLOLotDLTTrendFragment);
    }

    //切换Fragment
    private void switchFragment(Fragment targetFragment) {
        if (currentFragment != targetFragment) {
            mTransaction = getChildFragmentManager().beginTransaction();

            if(targetFragment.isAdded()) {
                mTransaction.hide(currentFragment).show(targetFragment);
            } else {
                mTransaction.hide(currentFragment).add(R.id.frame_lot_trend,targetFragment);
            }

            currentFragment = targetFragment;

            mTransaction.commit();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_ssq://双色球
                switchToleft();
                break;
            case R.id.rb_dlt://大乐透
                switchToRight();
                break;
            default:
                break;
        }
    }

    private void showIntro() {
        if(intro == null) {
            InputStream in = mContext.getResources().openRawResource(R.raw.intro_trend);
            BufferedReader br;
            try {
                br = new BufferedReader(new InputStreamReader(in, "utf-8"));

                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line.trim() + "\n");
                }

                intro = sb.toString();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        show(intro);
    }

    void show(String intro) {
        AlertDialog dialog = new AlertDialog.Builder(mContext)
                .setTitle("参考说明")
                .setMessage(intro)
                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                }).create();
        dialog.show();
    }
}
